﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.EventBroker;

namespace AddRemoveSubscription
{
    public partial class Shell : Form
    {
        public Shell()
        {
            InitializeComponent();
        }

        private WorkItem rootWorkItem;
        [ServiceDependency]
        public WorkItem RootWorkItem
        {
            set { rootWorkItem = value; }
        }

        private void FireEvent_Click(object sender, EventArgs e)
        {
            rootWorkItem.EventTopics["MyEvent"].Fire(this, EventArgs.Empty, null, PublicationScope.Global);
        }

        private void AddSubscription_Click(object sender, EventArgs e)
        {
            Subscriber subscriber = (Subscriber)rootWorkItem.Items["Subscriber"];
            rootWorkItem.EventTopics["MyEvent"].AddSubscription(subscriber, "MyEventHandler", rootWorkItem, ThreadOption.Publisher);
            AddSubscription.Enabled = false;
            RemoveSubscription.Enabled = true;
        }

        private void RemoveSubscription_Click(object sender, EventArgs e)
        {
            Subscriber subscriber = (Subscriber)rootWorkItem.Items["Subscriber"];
            rootWorkItem.EventTopics["MyEvent"].RemoveSubscription(subscriber,"MyEventHandler");
            AddSubscription.Enabled = true;
            RemoveSubscription.Enabled = false;
        }
    }
}
